package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tako.wisers.com.android.jdo.Geohash;
import tako.wisers.com.android.jdo.StationJdo;

public class ParserData {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		String str = IOUtils.toString(new FileInputStream("F:/and/data.txt"), "gbk");
		try {
			JSONArray ja = new JSONArray(str);
			int len = ja.length();
			Geohash gh = new Geohash();
			StringBuffer sb = new StringBuffer("");
			for(int i=0;i<len;i++){
				JSONObject obj = ja.getJSONObject(i);
				String phone = obj.getString("phone").replace(",", " ");
				String addr = obj.getString("addr").replace(",", " ");
				double lon = obj.getDouble("long");
				double lat = obj.getDouble("lat");
				String photo = obj.getString("photo");
				String url = obj.getString("url");
				String name = obj.getString("name").replace(",", " ");
				String info = obj.getString("categories").replace("[", "").replace("]", "").replace("\"", "").replace(",", "，");
				StationJdo jdo = new StationJdo(name, addr, "香港",
												"HK", phone, 1, lat, lon,
												gh.encode(lat, lon), url, info, photo,0,0);
				sb.append(jdo.toString() + "\r\n");
			}
			IOUtils.write(sb, new FileOutputStream("F:/and/food.txt"), "utf-8");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
